package com.scm.web.controller.product;


import com.scm.common.annotation.Log;
import com.scm.common.core.controller.BaseController;
import com.scm.common.core.domain.AjaxResult;
import com.scm.common.core.domain.entity.SysUser;
import com.scm.common.core.page.PageDomain;
import com.scm.common.core.page.TableDataInfo;
import com.scm.common.core.page.TableSupport;
import com.scm.common.enums.BusinessType;
import com.scm.common.utils.poi.ExcelUtil;
import com.scm.dto.resp.product.ScmProductDetailOmsDo;
import com.scm.dto.resp.product.ScmProductDo;
import com.scm.dto.resp.product.SelectProductPageResp;
import com.scm.persist.model.ScmProduct;
import com.scm.persist.model.ScmProject;
import com.scm.service.IScmProductService;
import com.scm.service.IScmProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/scm/product")
public class ScmProductController  extends BaseController {

    private String prefix = "product";

    @Autowired
    private IScmProductService scmProductService;

    @Autowired
    private IScmProjectService scmProjectService;

    @GetMapping("/popupListPage")
    public String detail( ModelMap mmap)
    {

        return prefix + "/popupList";
    }

    /**
     * 查询订单主列表
     */
    @RequiresPermissions("product:list")
    @PostMapping("/popupList")
    @ResponseBody
    public TableDataInfo popupList(ScmProduct scmProduct)
    {
        SysUser sysUser = getSysUser();

        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer start = (pageDomain.getPageNum() - 1) * 10;

        SelectProductPageResp resp = scmProductService.selectScmProductList(sysUser.getTenantId(),scmProduct,start,pageDomain.getPageSize());
        return toTableDataInfo(resp.getTotal(),resp.getList());

    }

    @PostMapping("/detail/{id}")
    @ResponseBody
    public AjaxResult detail(@PathVariable("id") Long id)
    {

        SysUser sysUser = getSysUser();

        ScmProductDo scmProduct = scmProductService.selectScmProductById(sysUser.getTenantId(),id);
        //ScmProject scmProject = scmProjectService.selectScmProjectById(sysUser.getTenantId(), id);
        return AjaxResult.success(scmProduct);

    }



    @RequiresPermissions("system:product:view")
    @GetMapping()
    public String product()
    {
        return prefix + "/product";
    }

    /**
     * 查询商品信息列表
     */
    @RequiresPermissions("system:product:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ScmProduct scmProduct)
    {
        SysUser sysUser = getSysUser();

        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer start = (pageDomain.getPageNum() - 1) * 10;

        SelectProductPageResp resp = scmProductService.selectScmProductList(sysUser.getTenantId(),scmProduct,start,pageDomain.getPageSize());
        return toTableDataInfo(resp.getTotal(),resp.getList());
    }

    /**
     * 导出商品信息列表
     */
    @RequiresPermissions("system:product:export")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ScmProduct scmProduct)
    {
        SysUser sysUser = getSysUser();


        SelectProductPageResp resp = scmProductService.selectScmProductList(sysUser.getTenantId(),scmProduct,0,10);
        ExcelUtil<ScmProduct> util = new ExcelUtil<ScmProduct>(ScmProduct.class);
        return util.exportExcel(resp.getList(), "商品信息数据");
    }

    /**
     * 新增商品信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商品信息
     */
    @RequiresPermissions("system:product:add")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave( ScmProductDo scmProductDo)
    {
        SysUser sysUser = getSysUser();

        return toAjax(scmProductService.insertScmProduct(sysUser,scmProductDo));
    }

    /**
     * 修改商品信息
     */
    @RequiresPermissions("system:product:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysUser sysUser = getSysUser();

        ScmProductDo scmProduct = scmProductService.selectScmProductById(sysUser.getTenantId(),id);
        mmap.put("scmProduct", scmProduct);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品信息
     */
    @RequiresPermissions("system:product:edit")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave( ScmProductDo scmProduct)
    {
        SysUser sysUser = getSysUser();

        return toAjax(scmProductService.updateScmProduct( sysUser,scmProduct));
    }

    /**
     * 删除商品信息
     */
    @RequiresPermissions("system:product:remove")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(scmProductService.deleteScmProductByIds(ids));
    }



}
